<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Persistent Connections (version 1.3.0+)</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="mongo.connecting.uds.html">« Domain Socket Support</a></li>
      <li style="float: right;"><a href="mongo.connecting.pools.html">Connection Pooling (version 1.2.0-1.2.12 *only*) »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="mongo.connecting.html">Connecting</a></li>
    <li>Persistent Connections (version 1.3.0+)</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="mongo.connecting.persistent" class="section">
  <h2 class="title">Persistent Connections (version 1.3.0+)</h2>

  <p class="para">
   All versions of the driver since 1.3.0 utilize persistent connections to
   minimize the number of connections made to each database server. These
   connections are saved by the PHP worker process and may be reused between
   multiple requests.
  </p>

  <p class="para">
   Before connecting to a database server, the driver will create a hash for the
   connection based on its host, port, replica set name (if any), any
   authentication credentials (e.g. username, password, database), and the
   process ID. If a connection already exists for that hash, it will be used in
   lieu of creating a new connection associated with that hash.
   <span class="function"><a href="mongoclient.getconnections.html" class="function">MongoClient::getConnections()</a></span> may be used to retrieve info
   about each persistent connection. Consider the following program:
  </p>

  <div class="example" id="mongo.connecting.persistent-example">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$m1&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(</span><span style="color: #DD0000">'mongodb://localhost'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$m2&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(</span><span style="color: #DD0000">'mongodb://localhost'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$m3&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(</span><span style="color: #DD0000">'mongodb://user:pw@localhost'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$m4&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(</span><span style="color: #DD0000">'mongodb://127.0.0.1'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$m5&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(</span><span style="color: #DD0000">'mongodb://rs1.local:30017,rs2.local:30018/?replicaSet=rs'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$m6&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">(</span><span style="color: #DD0000">'mongodb://sharding.local:40017'</span><span style="color: #007700">);<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">MongoClient</span><span style="color: #007700">::</span><span style="color: #0000BB">getConnections</span><span style="color: #007700">()&nbsp;as&nbsp;</span><span style="color: #0000BB">$conn</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$conn</span><span style="color: #007700">[</span><span style="color: #DD0000">'hash'</span><span style="color: #007700">],&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>The above example will output
something similar to:</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
localhost:27017;-;X;15487
localhost:27017;-;admin/user/c56c…8bbc;15487
127.0.0.1:27017;-;X;15487
rs1.local:30017;rs;X;15487
rs2.local:30018;rs;X;15487
sharding.local:40017;-;X;15487
</pre></div>
   </div>
  </div>

  <p class="para">
   In this example <code class="literal">$m1</code> and <code class="literal">$m2</code> have the
   same hash and share a persistent connection. Connections for each other
   MongoClient instance hash to unique values and use their own sockets. Note
   that &quot;localhost&quot; and &quot;127.0.0.1&quot; do not share the same hash; DNS resolution
   is not taken into account.
  </p>
 </div></div></div></body></html>